import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:jinke/page/pro_list/subs/pro_open/subs/pro_open_step1/view.dart';
import 'package:jinke/page/pro_list/subs/pro_open/subs/pro_open_step2/index.dart';
import 'package:jinke/page/pro_list/subs/pro_open/subs/pro_open_step3/index.dart';
import 'package:jinke/utils/component/component.dart';
import 'package:jinke/utils/component/selected_tag_view.dart';
import 'package:jinke/utils/util.dart';

import '../subs/pro_open_step1/controller.dart';
import 'index.dart';
import 'widgets/pro_brife.dart';
import 'widgets/widgets.dart';

class ProOpenPage extends GetView<ProOpenController> {
  const ProOpenPage({Key? key}) : super(key: key);

  // 主视图
  Widget _buildView() {
    return Column(
      children: [
        OpenStepHeader(),
        Expanded(
          child: PageView(
            physics: const NeverScrollableScrollPhysics(),
            controller: controller.state.pageCtrl,
            children: [
              ProOpenStep1Page(),
              ProOpenStep2Page(),
              ProOpenStep3Page()
            ],
          ),
        )
      ],
    );
  }

  Widget _getActionButton() {
    var page = controller.state.stepIndex;
    if (page == 0) {
      return ExpandedButton(
        onPressed: () async {
          final step1Ctrl = Get.find<ProOpenStep1Controller>();
          var canGoNext = await step1Ctrl.onNext();
          if (canGoNext) {
            controller.state.pageCtrl.jumpToPage(1);
          }
        },
        text: '下一步',
        backgroundColor: ColorsUtil.blue,
        borderradius: BorderRadius.zero,
      );
    }
    return Row(
      children: [
        Expanded(
          child: InkWell(
              onTap: () {
                controller.state.pageCtrl
                    .jumpToPage(controller.state.stepIndex - 1);
              },
              child: Container(
                color: Colors.white,
                height: 50.r,
                alignment: Alignment.center,
                child: Text(
                  '上一步',
                  style: TextStyle(
                    color: ColorsUtil.title,
                    fontSize: 15.sp,
                  ),
                ),
              )),
        ),
        Expanded(
          child: InkWell(
              onTap: () async {
                if (page == 1) {
                  final step2Ctrl = Get.find<ProOpenStep2Controller>();
                  var canGoNext = await step2Ctrl.onNext();
                  if (canGoNext) {
                    controller.state.pageCtrl.jumpToPage(2);
                  }
                } else {
                  final step3Ctrl = Get.find<ProOpenStep3Controller>();
                  var canGoNext = await step3Ctrl.onNext();
                  if (canGoNext) {
                    controller.open();
                  }
                }
              },
              child: Container(
                color: ColorsUtil.blue,
                height: 50.r,
                alignment: Alignment.center,
                child: Text(
                  page == 1 ? '下一步' : '创建',
                  style: TextStyle(
                    color: ColorsUtil.white,
                    fontSize: 15.sp,
                  ),
                ),
              )),
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ProOpenController>(
      builder: (_) {
        return Scaffold(
          appBar: detailAppBar(
            context,
            title: '项目创建',
            titleColor: ColorsUtil.title,
            leadingColor: ColorsUtil.title,
            backgroundColor: Colors.white,
          ),
          body: SafeArea(child: _buildView()),
          floatingActionButtonLocation:
              FloatingActionButtonLocation.centerDocked,
          floatingActionButton: _getActionButton(),
        );
      },
    );
  }
}
